﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BabyFood.Entities;
using BabyFood.Data;
using System.Drawing;

public partial class QuiDoi_CNQuiDoi : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["id"] != null)
            {
                H_CatalogId.Value = Request.QueryString["id"].ToString();
                BindData(Convert.ToInt32(H_CatalogId.Value));
            }
        }
    }
    /// <summary>
    /// Hàm dựng tên file upload
    /// </summary>
    public string Uploadedfilename { get { return Convert.ToString(ViewState["filename"]); } set { ViewState["filename"] = value; } }

    protected void BindData(int IdQuiDoi)
    {
        QuiDoi entQuiDoi = DataRepository.QuiDoiProvider.GetById(IdQuiDoi);
        txtTenQC.Text = entQuiDoi.Ten;
        chkTinNong.Checked = entQuiDoi.TrangThai;
        lblMsg.Text = entQuiDoi.LienKet;
        txtDienGiai.Text = entQuiDoi.DienTa;
    }
    /// <summary>
    /// Hàm Upload PDF
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        try
        {
            if (fup.HasFile)
            {
                string strFilePath = Server.MapPath("~/PDF/");
                Uploadedfilename = Convert.ToString(Guid.NewGuid()) + ".pdf";
                strFilePath = strFilePath + Uploadedfilename;
                fup.SaveAs(strFilePath);
                lblMsg.Text += "File Name :" + strFilePath;
                lblMsg.Text += "<br>File Type :" + fup.PostedFile.ContentType;
                lblMsg.Text += "<br>File Size :" + fup.PostedFile.ContentLength;
                lblMsg.ForeColor = Color.Blue;
                H_TepTin.Value = "~/Pdf/" + Uploadedfilename;
                lblMsg.Text = "PDF File Upload  Successfully";
            }
            else
            {

                lblMsg.Text = "Please Upload PDF File";
            }
        }
        catch (Exception ex)
        {
            Common.WriteLog("CNQuiDoi.aspx.cs", "btnUpload_Click", ex.ToString());
            WebMessage.Show("Upload không thành công!");
        }
    }

    protected void cmdLuu_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            QuiDoi entQuiDoi = new QuiDoi();
            entQuiDoi.Id = Convert.ToInt32(H_CatalogId.Value);
            entQuiDoi.Ten = Common.StandardString(txtTenQC.Text);
            entQuiDoi.DienTa = Common.StandardString(txtDienGiai.Text);

            entQuiDoi.TrangThai = chkTinNong.Checked;

            //Upload pdf

            entQuiDoi.LienKet = Common.StandardString(H_TepTin.Value);

            if (entQuiDoi.Id == 0)
            {
                updateStatus = DataRepository.QuiDoiProvider.Insert(entQuiDoi);
                cmdNhapTiep_Click(sender, e);
            }
            else
            {

                entQuiDoi.EntityState = EntityState.Changed;
                updateStatus = DataRepository.QuiDoiProvider.Update(entQuiDoi);
                cmdNhapTiep_Click(sender, e);
            }

            if (!updateStatus)
                WebMessage.Show("Cập nhật Qui đổi thất bại!");
            else
            {
                WebMessage.Show("Cập nhật Qui đổi thành công!");
                H_CatalogId.Value = entQuiDoi.Id.ToString();
            }

        }
        catch (Exception ex)
        {
            Common.WriteLog("CNQuiDoi.aspx.cs", "cmdLuu_Click", ex.ToString());
            WebMessage.Show("Cập nhật Qui đổi thất bại!");
        }
    }

    protected void cmdNhapTiep_Click(object sender, ImageClickEventArgs e)
    {
        H_CatalogId.Value = "0";
        txtTenQC.Text = "";
        lblMsg.Text = "";
        chkTinNong.Checked = true;
        txtDienGiai.Text = "";
    }

}